package com.goktech.olala.client.controller.index;

import com.alibaba.fastjson.JSONObject;
import com.goktech.olala.core.resp.RespBrandVo;
import com.goktech.olala.core.resp.RespCategoryVo;
import com.goktech.olala.core.service.*;
import com.goktech.olala.server.pojo.goods.Activit;
import com.goktech.olala.server.pojo.goods.Advers;
import com.goktech.olala.server.pojo.goods.Article;
import com.goktech.olala.server.pojo.goods.Login;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther : 朱正标
 * @create time : 2020/11/29 16:34
 * @description :
 */
@RestController
public class IndexController {

    @Autowired
    IArticleService articleService;
    @Autowired
    IAdversService adversService;
    @Autowired
    IActivitService activitService;
    @Autowired
    ILoginService loginService;

    @Autowired
    IGoodCategory goodCategory;

    @GetMapping("/")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("redirect:index/init.do");
        return mv;
    }
    @RequestMapping("/index/init.do")
    public ModelAndView toIndex(){
        ModelAndView mv = new ModelAndView();

        List<Article> articles = articleService.selectAll();
        List<Advers> advers = adversService.selectAll();
        List<Activit> activits = activitService.selectAll();
        List<RespCategoryVo> respCategoryVos = goodCategory.beTreeList();

        for (RespCategoryVo first:respCategoryVos) {
            List<RespBrandVo> brands = goodCategory.selectBrand();
            if (brands!=null){
                first.setBrandList(brands);
            }
        }

        //将查询的热门头条list数据转发到首页
        mv.addObject("articleVoList",articles);
        //将查询的轮播图list数据转发到首页
        mv.addObject("ctmAdvers",advers);
        //将热门活动list数据转发到首页
        mv.addObject("indexActivities",activits);
        //一级目录
        mv.addObject("categoryList",respCategoryVos);


        mv.setViewName("home/index");
        return mv;
    }
    /**
     * 登录
     */
    @PostMapping("/cntApi/login.do")
    public void toLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/json;charset=utf-8");
        ModelAndView mv = new ModelAndView();
        //获取用户填写的用户名
        String name = req.getParameter("userName");
        //获取用户填写的密码
        String psw = req.getParameter("password");
        PrintWriter out = resp.getWriter();
        //声明一个json对象
        JSONObject json = new JSONObject();


        List<Login> logins = loginService.selectAll();
        //如果输入用户的输入不为空
        if(name!=null&&!"".equals(name)&&psw!=null&&!"".equals(psw)){
            for (Login login:logins) {
                //如果查询到有这个用户名和密码，可以登录
                if(login.getLoginName().equals(name)&&login.getPassword().equals(psw)){
                    json.put("code",10000);
                }else {
                    json.put("code",-10000);
                }
            }
        }else {
            //输入的用户名和密码为空，code为0
            json.put("code",0);
        }
        out.print(json);
        out.flush();
        out.close();
    }

    /**
     * 用户中心
     */
    @RequestMapping("/cntApi/info.do")
    public ModelAndView toUserCenter(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("home/head");
        return mv;
    }
}
